package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.listener.SxfExcelListener;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtySxf;
import cn.stylefeng.guns.sys.modular.system.mapper.BtySxfMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyJtgzParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtySxfParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtySxfResult;
import cn.stylefeng.guns.sys.modular.system.service.BtySxfService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**
 * 控制器
 *
 * @author liuy
 * @Date 2022-03-02 14:38:02
 */
@Controller
@RequestMapping("/btySxf")
public class BtySxfController extends BaseController {

    private String PREFIX = "/modular/system//btySxf";

    @Autowired
    private BtySxfService btySxfService;
    @Resource
    private BtySxfMapper sxfMapper;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2022-03-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btySxf.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2022-03-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btySxf_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2022-03-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btySxf_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2022-03-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtySxfParam btySxfParam) {
        this.btySxfService.add(btySxfParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2022-03-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtySxfParam btySxfParam) {
        this.btySxfService.update(btySxfParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2022-03-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtySxfParam btySxfParam) {
        this.btySxfService.delete(btySxfParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2022-03-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtySxfParam btySxfParam) {
        BtySxf detail = this.btySxfService.getById(btySxfParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2022-03-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtySxfParam btySxfParam) {
        return this.btySxfService.findPageBySpec(btySxfParam);
    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUsers(@RequestParam("file") MultipartFile file, BtyJtgzParam param) {
        JsonResult jsonResult = new JsonResult();
        if(ToolUtil.isEmpty(param.getZdrq())){
            jsonResult.setState(0);
            jsonResult.setErrmsg("请先选择制单日期！");
            return jsonResult;
        }
        //导入之前删掉之前的数据
        sxfMapper.delete(new QueryWrapper<>());
        String zdrq=param.getZdrq();
        try {
            SxfExcelListener jtgzExcelListener=new SxfExcelListener(btySxfService);
            EasyExcel.read(file.getInputStream(), BtySxf.class, jtgzExcelListener).sheet(0).doRead();
            //导入成功之后生成微信支付宝手续费凭证
            List<BtySxfResult>list=btySxfService.sxfList();
            for (BtySxfResult sxfResult : list) {
                BigDecimal wxsxf=sxfResult.getWxsxf();
                BigDecimal zfbsxf=sxfResult.getZfbsxf();
                //用友大区编号
                String bh=sxfResult.getCode();
                //妖码
                String yaoma=sxfResult.getYaoma();
                //店名
                String dm=sxfResult.getDm();
                RestTemplate restTemplate = new RestTemplate();
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
                headers.add("trantype", "code");
                // 系统编码
                headers.add("system", "001");
                // 用户
                headers.add("usercode", "15253172037");
                headers.add("password", "514576f58a89767b46c1b44767a08f77");
                headers.add("Accept", MediaType.APPLICATION_JSON.toString());

                StringBuilder sb=new StringBuilder();
                sb.append("{\n" +
                        "    \"voucher\":[\n" +
                        "        {\n" +
                        "            \"details\":[");
                if(wxsxf.compareTo(BigDecimal.ZERO)==1){
                //微信手续费凭证
                sb.append("                {\n" +
                        "                    \"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +//部门档案辅助核算编码
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\"\",\n" +
                        "                    \"debitamount\":\""+wxsxf+"\",\n" +//借方金额
                        "                    \"explanation\":\"微信手续费\",\n" +//借方摘要
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"660306\",\n" +//借方科目编码
                        "                    \"pk_currtype\":\"CNY\"\n" +
                        "                }," +
                        "{\n" +
                        "\t\t\t\t\"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'"+yaoma+"'\n" +//部门档案辅助核算编码
                        "                        },\n" +
                        "\t\t\t\t\t\t{\n" +
                        "                            \"checktypecode\":\"7\",\n" +
                        "                            \"checkvaluecode\":\"0172\"\n" + //微信收款编号
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\""+wxsxf+"\",\n" +//贷方金额
                        "                    \"creditquantity\":\"\",\n" +
                        "                    \"debitamount\":\"\",\n" +
                        "                    \"debitquantity\":\"\",\n" +
                        "                    \"detailindex\":\"\",\n" +
                        "                    \"excrate1\":\"\",\n" +
                        "                    \"explanation\":\"微信手续费\",\n" +//贷方摘要
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"112206\",\n" +//贷方科目编码
                        "                    \"pk_currtype\":\"CNY\",\n" +
                        "                    \"price\":\"\"\n" +
                        "                },\n");
                 }
                if(zfbsxf.compareTo(BigDecimal.ZERO)==1){
                    //微信手续费凭证
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'"+yaoma+"'\n" +//部门档案辅助核算编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+zfbsxf+"\",\n" +//借方金额
                            "                    \"explanation\":\"支付宝手续费\",\n" +//借方摘要
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"660306\",\n" +//借方科目编码
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                }," +
                            "{\n" +
                            "\t\t\t\t\"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'"+yaoma+"'\n" +//部门档案辅助核算编码
                            "                        },\n" +
                            "\t\t\t\t\t\t{\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":\"0189\"\n" + //支付宝收款编号
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\""+zfbsxf+"\",\n" +//贷方金额
                            "                    \"creditquantity\":\"\",\n" +
                            "                    \"debitamount\":\"\",\n" +
                            "                    \"debitquantity\":\"\",\n" +
                            "                    \"detailindex\":\"\",\n" +
                            "                    \"excrate1\":\"\",\n" +
                            "                    \"explanation\":\"支付宝手续费\",\n" +//贷方摘要
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112207\",\n" +//贷方科目编码
                            "                    \"pk_currtype\":\"CNY\",\n" +
                            "                    \"price\":\"\"\n" +
                            "                },\n");
                }
                sb.append("            ],\n" +
                        "            \"pk_corp\":"+bh+",\n" +//用友大区编号
                        "            \"pk_glorgbook\":\""+bh+"-bty01\",\n" +
                        "            \"pk_prepared\":\"15253172037\",\n" +
                        "            \"pk_vouchertype\":\"记账\",\n" +
                        "            \"prepareddate\":\""+zdrq+"\"\n" +//用友制单日期
                        "        }\n" +
                        "    ]\n" +
                        "}");
                //System.out.println("sb:"+sb.toString());
                HttpEntity<String> formEntity = new HttpEntity<String>(sb.toString(), headers);
                String url = "http://124.70.28.147:8089/u8cloud/api/gl/voucher/insert";
                String result = restTemplate.postForEntity(url, formEntity, String.class).getBody();
                //System.out.println("result:"+result);
                JsonParser jp = new JsonParser();
                //将json字符串转化成json对象
                JsonObject jo = jp.parse(result).getAsJsonObject();
                //获取status对应的值
                String status = jo.get("status").getAsString();

                if(status.equals("falied")){
                    String errormsg = jo.get("errormsg").getAsString();
                    BtySxf sxf=btySxfService.getOne(new QueryWrapper<BtySxf>().eq("dm",dm));
                    if(ToolUtil.isNotEmpty(sxf)){
                        sxf.setErrormsg(errormsg);
                        btySxfService.updateById(sxf);
                    }
                }else{
                    BtySxf sxf=btySxfService.getOne(new QueryWrapper<BtySxf>().eq("dm",dm));
                    if(ToolUtil.isNotEmpty(sxf)){
                        sxf.setFlag(1);
                        btySxfService.updateById(sxf);
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return true;
    }

}


